<?php
namespace app\Manage\controller;

use app\common\controller\Manage;

use app\common\model\ManageUserRole;
use app\common\model\ManageUserRoleRel;
use app\common\model\ManageUserRoleOperationRel;
use think\facade\Log;

use Request;


class UserRole extends Manage
{
    public function index()
    {
        if(Request::isAjax()){
            $manageRoleModel = new ManageUserRole();
            $data = input('param.');
            return $manageRoleModel->tableData($data);
        }else{
            return $this->fetch('index');
        }
    }

    public function add()
    {
        $this->view->engine->layout(false);
        $manageRoleModel = new ManageUserRole();
        if(Request::isPost()){
            if(!input('?param.name')){
                return error_code(11070);
            } 

            // 判断是否有同名的角色
            $data = input('param.');
            $userRoleInfo  = $manageRoleModel->where('name', $data['name'])->find();
           
            if ($userRoleInfo) {
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '已存在同名角色！'
                ];
            }

            $manageRoleModel->save($data);
            \think\facade\Request::token('__ZlServer_Token__', 'sha1');
            return [
                'status' => true,
                'data' => '',
                'msg' => '添加成功'
            ];

        }
        return $this->fetch('edit');
    }
    public function del()
    {
        if(!input('?param.id')){
            return error_code(10000);
        }

        // id 在 5以内为保留角色不允许删除
        // if(input('param.id') <= 5){
        //     return error_code(11106);
        // }

        // 这里需要清理缓存
        if(cache('?user_operation_'.input('param.id'))){
            cache('user_operation_'.input('param.id'), null);
        }
 
        $ManageRoleModel = new ManageUserRole();
        return $ManageRoleModel->toDel(input('param.id'));
    }

    public function getOperation()
    {
        $return_data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [],
        ];

        if (!input('?param.id')) {
            return error_code(10000);
        }
 
        // id 在 5以内为保留角色不允许删除
        // if(input('param.id') <= 5){
        //     return error_code(11107);
        // }

        $manageRoleModel = new ManageUserRole();
        $re              = $manageRoleModel->getRoleOperation(input('param.id/d'));
        if (!$re['status']) {
            return $re;
        }
        $return_data['data'] = $re['data'];
        return $return_data;
    }

    public function savePerm(){
        $post = input('param.');

        if(!isset($post['id'])){
            return error_code(10000);
        }

        // 前台什么都不选
        // if(!isset($post['data'])){
        //     return error_code(10000);
        // }
        
        //保存角色信息
        $manageRoleModel = new ManageUserRole();
        $manageRoleInfo = $manageRoleModel->where(['id'=>$post['id']])->find();
        if(!$manageRoleInfo){
            return error_code(11071);
        }
        $mrorModel = new ManageUserRoleOperationRel();
 
        // 找出所有关联当前角色的用户 
        $manageUserRoleRelModel = new ManageUserRoleRel();
        $manageUserInfoList = $manageUserRoleRelModel->where(['role_id '=>$post['id']])->select();
        $manageUserInfoArray = $manageUserInfoList->toArray();
 
        foreach ($manageUserInfoArray as $key => $val) { 
            // 清理缓存
            if(cache('?user_operation_'. $val['manage_id'] )){
                cache('user_operation_'. $val['manage_id'] , null);
            }
        } 
        
        $mrorModel->savePerm($post['id'],$post['data']);
        return [
            'status' => true,
            'data' => '',
            'msg' => '设置成功'
        ];
    }
}